app.controller('brandController', function ($scope, $controller, $location, brandService) {

    $controller('baseController', {$scope: $scope});

    $scope.searchEntity = {};

    $scope.entity = {brandName: null, taxNumber: null, brandRate: null, stepRateBrandList: [{prefix:0,suffix:0,personalTax:0,valueAddedTax:0,laborServiceFee:0}]};


    //分页获取数据
    $scope.findPage = function (pageNum, pageSize) {

        if (pageNum == null)
            pageNum = 1;

        if (pageSize == null)
            pageSize = 10;


        brandService.findPage(pageNum, pageSize, $scope.searchEntity).success(function (response) {

            if (response.code == 200) {
                $scope.brandList = response.data;

                //更新总记录数
                $scope.paginationConf.totalItems = response.total;

            } else {
                alert(response.msg);
            }

        });

    };

    //添加或更新
    $scope.insertOrUpdate = function () {

        console.log($scope.isClick);
        if ($scope.isClick) {
            console.log("重复提交");
            return;
        }
        //标记为点击状态
        $scope.isClick = true;


        if ($scope.isNull($scope.entity.brandName)) {
            alert("未设置品牌名称");

            //还原按钮初始状态
            $scope.isClick = false;

            return
        }

        if ($scope.isNull($scope.entity.companyName)) {
            alert("未设置公司名称");

            //还原按钮初始状态
            $scope.isClick = false;

            return
        }

        if ($scope.isNull($scope.entity.taxNumber)) {
            alert("未设置税号");

            //还原按钮初始状态
            $scope.isClick = false;

            return
        }

        if ($scope.isNull($scope.entity.brandRate)) {
            alert("未设置品牌费率");

            //还原按钮初始状态
            $scope.isClick = false;

            return
        }

        if ($scope.isNull($scope.entity.brandRateTwo)) {
            alert("未设置品牌二档费率");

            //还原按钮初始状态
            $scope.isClick = false;

            return
        }
        var stepRateBrandList = $scope.entity.stepRateBrandList;

        if (stepRateBrandList == null || stepRateBrandList.length == 0){
            alert("未设置阶梯费率");
            $scope.isClick = false;
        }

        for (var i = 0; i < stepRateBrandList.length; i++) {

            if (i == 0){

                if (stepRateBrandList[i].prefix == null ||  stepRateBrandList[i].prefix < 0) {
                    alert("第 "+(i + 1)+" 阶梯前缀错误");

                    $scope.isClick = false;
                    break;
                }

            }

            if (i != 0){

                if (stepRateBrandList[i].prefix == null ||  stepRateBrandList[i].prefix <= 0) {
                    alert("第 "+(i + 1)+" 阶梯前缀错误");

                    $scope.isClick = false;
                    break;
                }

            }

            if (stepRateBrandList[i].suffix == null ||  stepRateBrandList[i].suffix <= 0) {
                alert("第 "+(i + 1)+" 阶梯后缀错误");

                $scope.isClick = false;
                break;
            }

        }


        var serviceObj = null;

        if ($scope.entity.id == null) {//新增
            serviceObj = brandService.insert($scope.entity);
        } else {//更新
            serviceObj = brandService.update($scope.entity);
        }

        serviceObj.success(function (response) {
            if (response.code = 200) {
                location.href = 'brand-manage.html';
            } else {

            }

            //还原按钮初始状态
            $scope.isClick = false;
        }).error(function () {
            //还原按钮初始状态
            $scope.isClick = false;
        });

    };

    //根据id获取品牌信息
    $scope.findOne = function () {


        var brandId = $location.search()['brandId'];

        if ($scope.isNull(brandId))
            return;


        brandService.findOne(brandId).success(function (response) {

            if (response.code == 200) {
                $scope.entity = response.data;
                var stepRateBrandList = $scope.entity.stepRateBrandList;

                if (stepRateBrandList == null || stepRateBrandList.length == 0){

                    $scope.entity.stepRateBrandList = [{prefix:0,suffix:0,personalTax:0,valueAddedTax:0,laborServiceFee:0}];

                } else {

                    for (var i = 0; i < stepRateBrandList.length; i++) {
                        console.log(stepRateBrandList[i].prefix/10000)
                        //前缀
                        stepRateBrandList[i].prefix = stepRateBrandList[i].prefix/10000;

                        //后缀
                        stepRateBrandList[i].suffix = stepRateBrandList[i].suffix/10000;

                        //服务费
                        stepRateBrandList[i].laborServiceFee = (stepRateBrandList[i].laborServiceFee * 100).toFixed(2);

                    }

                }
            } else {
                alert(response.msg);
            }

        });

    };


    //重置查询参数
    $scope.resetSearchEntity = function () {

        $scope.searchEntity = {};

        $scope.reloadList();
    };

    //接口秘钥下载
    $scope.secretKeyDownload = function (brandId) {
        if (!confirm("确认下载此秘钥吗？")) {
            return;
        }
        brandService.secretKeyDownload(brandId).success(function (res) {
            if (res.code == 200) {
                window.open(res.data);
            } else {
                alert(res.msg);
            }
        })
    }

    //标记阶梯数
    $scope.indexStepRateBrandList = ["StepRate"];


    //添加阶梯级数
    $scope.updateIndexStepRateBrandList = function(){
        console.log("添加执行了");

        $scope.indexStepRateBrandList.push("StepRate");

    };


    //增加阶梯级数
    $scope.createStepList = function(){


        var stepRate = {prefix:0,suffix:0,personalTax:0,valueAddedTax:0,laborServiceFee:0};

        var addLength = $scope.indexStepRateBrandList.length - $scope.entity.stepRateBrandList.length;


        for (var j = 0; j < addLength; j++) {//循环选中规格的结果集


            $scope.entity.stepRateBrandList.push(stepRate);


        }

    };

    //删除阶梯费率的一行
    $scope.deletestepRateRow = function (index) {

        if ($scope.entity.stepRateBrandList.length == 1)
            return;

        $scope.entity.stepRateBrandList.splice(index,1);

        $scope.indexStepRateBrandList.splice(index,1);
    };

});